package com.ztools.pc.service.impl.store;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.store.Commodity;
import com.ztools.common.model.pc.store.StoreStock;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.store.CommodityMapper;
import com.ztools.pc.service.store.CategoryService;
import com.ztools.pc.service.store.CommodityService;
import com.ztools.pc.service.store.StoreStockService;
import com.ztools.pc.service.sys.DictService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:一口香菜
 * @Date: 2023/3/26  19:17
 * @Version 1.0
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {

    @Resource
    private CategoryService categoryService;
    @Resource
    private DictService dictService;
    @Lazy
    @Resource
    private StoreStockService storeStockService;

    @Override
    public Page<Commodity> findList(PageUtil<Commodity> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<Commodity> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<Commodity> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<Commodity> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public boolean delete(Commodity commodity) {
        boolean b = false;
        Commodity userById = getById(commodity.getId());
        b = removeById(commodity.getId());
        return b;
    }

    @Override
    public boolean add(Commodity commodity) {
        return save(commodity);
    }

    @Override
    public boolean update(Commodity commodity) {
        boolean b = false;
        // 修改前先获取，在删除（乐观锁）
        Commodity categoryById = getById(commodity);
        b = updateById(commodity);
        return b;
    }


    /**
     * 条码、自编码、商品名称进行模糊匹配
     *
     * @param value value
     * @return Commodity
     */
    @Override
    public List<Commodity> fuzzyQuery(String value) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        if (!"All".equals(value)) {
            queryWrapper.lambda().like(Commodity::getName, value)
                    .or().like(Commodity::getBarcode, value)
                    .or().like(Commodity::getCustomCode, value);
        }
        List<Commodity> list = list(queryWrapper);
        dictionary(list);
        return list;
    }

    @Override
    public List<Commodity> findByObject(Commodity commodity) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>(commodity);
        List<Commodity> list = list(queryWrapper);
        dictionary(list);
        return list;
    }

    @Override
    public List<Commodity> findByName(String name) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(Commodity::getName, name);
        return list(queryWrapper);
    }

    /**
     * 拼接条件
     *
     * @param commodity category
     * @return
     */
    public QueryWrapper<Commodity> getQueryWrapper(Commodity commodity) {
        QueryWrapper<Commodity> gradeQueryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(commodity.getName())) {
            gradeQueryWrapper.like("name", commodity.getName());
        }
        if (StrUtil.isNotEmpty(commodity.getBarcode())) {
            gradeQueryWrapper.like("barcode", commodity.getBarcode());
        }
        if (StrUtil.isNotEmpty(commodity.getCustomCode())) {
            gradeQueryWrapper.like("custom_code", commodity.getCustomCode());
        }
        if (commodity.getType() != null) {
            gradeQueryWrapper.like("type", commodity.getType());
        }
        return gradeQueryWrapper;
    }

    /**
     * 字典翻译
     *
     * @param commodityList 对象集合
     */
    public void dictionary(List<Commodity> commodityList) {
        for (Commodity commodity : commodityList) {
            dictionary(commodity);
        }
    }

    public void dictionary(Commodity commodity) {
        if (commodity != null) {
            // 商品分类翻译
            if (commodity.getType() != null) {
                commodity.setTypeName(categoryService.getById(commodity.getType()).getName());
            }
            // 商品类别翻译
            if (StrUtil.isNotEmpty(commodity.getCategory())) {
                commodity.setCategoryName(dictService.findByTypeCode(DictUtil.CATEGORY, commodity.getCategory()));
            }
            // 商品单位翻译
            if (StrUtil.isNotEmpty(commodity.getUnit())) {
                commodity.setUnitName(dictService.findByTypeCode(DictUtil.UNIT, commodity.getUnit()));
            }
            // 商品库存
            if (commodity.getId() != null) {
                StoreStock storeStock = new StoreStock();
                storeStock.setCommodityId(commodity.getId());
                List<StoreStock> storeStockList = storeStockService.findByObject(storeStock);
                if (storeStockList.size() > 0) {
                    commodity.setQuantity(storeStockList.get(0).getQuantity());
                }
            }
        }
    }

}
